#ifndef RECT_GRAPHIC_ITEM
#define RECT_GRAPHIC_ITEM

#include <QtGui/QGraphicsRectItem>
#include <QtGui/QPainter>
#include <QtGui/QStyleOptionGraphicsItem>

namespace gui
{

class RectGraphicItem : public QGraphicsRectItem
{
public:
    RectGraphicItem(qreal radius, bool antiAlias = true,
        bool defaultSelection = false, QGraphicsItem* parent = 0) :
            QGraphicsRectItem(parent),
            m_radius(radius),
            m_aa(antiAlias),
            m_defaultSelection(defaultSelection)
    {
    }

    RectGraphicItem(const QRectF& rect, qreal radius, bool antiAlias = true,
        bool defaultSelection = false, QGraphicsItem* parent = 0) :
            QGraphicsRectItem(rect, parent),
            m_radius(radius),
            m_aa(antiAlias),
            m_defaultSelection(defaultSelection)
    {
    }

    virtual ~RectGraphicItem() {}

protected:
    virtual void paint(QPainter* painter,
        const QStyleOptionGraphicsItem* option, QWidget* widget)
    {
        QStyleOptionGraphicsItem _option = QStyleOptionGraphicsItem(*option);

        if (!m_defaultSelection)
        {
            _option.state &= ~QStyle::State_Selected;
        }

        if (m_aa)
        {
            painter->setRenderHint(QPainter::Antialiasing);
        }

        if (m_radius < 0 || (m_radius - 0) < 0.0025)
        {
            QGraphicsRectItem::paint(painter, &_option, widget);
        }
        else
        {
            QPen _pen = pen();

            painter->setBrush(brush());

            if ((_option.state & QStyle::State_Selected) &&
                m_defaultSelection)
            {
                _pen.setStyle(Qt::DashLine);
            }

            painter->setPen(_pen);

            painter->drawRoundedRect(rect(), m_radius, m_radius);
        }
    }

    bool antialiasing() const
    {
        return m_aa;
    }

    void setAntialiasing(bool enabled)
    {
        m_aa = enabled;
    }

    bool defaultSelection() const
    {
        return m_defaultSelection;
    }

    void setDefaultSelection(bool enabled)
    {
        m_defaultSelection = enabled;
    }

    void setRadius(qreal radius)
    {
        m_radius = radius;
    }

    qreal radius() const
    {
        return m_radius;
    }

private:
    qreal m_radius;
    bool m_aa;
    bool m_defaultSelection;
};

} // namespace gui

#endif
